#include "CommConversion.h"
#include "ui_CommConversion.h"
#include "Log.h"
#include "Channels.h"
#include "SocketSystem.h"
#include "Application.h"
#include "TCPServerDlg.h"
#include "TCPClientDlg.h"
#include "UDPDlg.h"
#include "FileDirDlg.h"
#include "DataTypeDlg.h"

CommConversion::CommConversion(QWidget* parent) : 
	QMainWindow(parent),
	current_btn{ Qt::NoButton },
	current_mouse_point{}
{
	ui.setupUi(this);
	this->setWindowFlags(Qt::FramelessWindowHint);
	this->setAttribute(Qt::WA_TranslucentBackground);
	this->setWindowIcon(QIcon{":/low_icon_png"});
	this->setWindowTitle(ui.lab_title->text());

	connect(ui.btn_server, &QPushButton::clicked, this, &CommConversion::slot_server_click);
	connect(ui.btn_client, &QPushButton::clicked, this, &CommConversion::slot_client_click);
	connect(ui.btn_udp, &QPushButton::clicked, this, &CommConversion::slot_udp_click);
	connect(ui.btn_file, &QPushButton::clicked, this, &CommConversion::slot_file_click);
	connect(ui.btn_datatype, &QPushButton::clicked, this, &CommConversion::slot_datatype_click);
	
	connect(ui.btn_close, &QPushButton::clicked, this, &CommConversion::close);
	connect(ui.btn_restore, &QPushButton::clicked, this, &CommConversion::slot_switch_max);
	connect(ui.btn_min, &QPushButton::clicked, this, &CommConversion::showMinimized);

	//auto layout = new QGraphicsLinearLayout(Qt::Orientation::Horizontal);
	//ui.channel_view->emplase_channel();

	//btn->resize(80, 80);
	//scene->addItem(btn);
	
}

void CommConversion::mousePressEvent(QMouseEvent* event)
{
	this->current_btn = event->button();
	this->current_mouse_point = event->globalPos() - this->pos();
}

void CommConversion::mouseReleaseEvent(QMouseEvent* event)
{
	if(event->button() == this->current_btn)
	{
		this->current_btn = Qt::NoButton;
	}
}

void CommConversion::mouseMoveEvent(QMouseEvent* event)
{
	if(current_btn == Qt::LeftButton)
	{
		this->move(event->globalPos() - this->current_mouse_point);
	}
}

void CommConversion::slot_server_click()
{
	//Logger().info("Say Hello");

	//ChannelCreateInfo info{};
	//info.type = CHANNEL_PROTO_TYPE_TCP_SERVER;

	//info.input_address.init("192.168.101.4", 7274);

	//ChannelCreateInfo c_info{};
	//c_info.type = CHANNEL_PROTO_TYPE_TCP_CLIENT;
	//c_info.input_address.init_client(1702);
	//c_info.output_address.init("192.168.101.4", 1701);

	//ChannelCreateInfo udp_info{};
	//udp_info.type = CHANNEL_PROTO_TYPE_UDP;
	//udp_info.input_address.init_client(7274);

	////tcp = CreateChannel(&info);
	////client = CreateChannel(&c_info);
	//udp = CreateChannel(&udp_info);

	TCPServerDlg dlg{};
	while(dlg.showDialog())
	{
		ChannelCreateInfo info{};
		if(dlg.get_info(info))
		{
			ui.channel_view->emplase_channel(&info);
			break;
		}
	}
}

void CommConversion::slot_client_click()
{
	TCPClientDlg dlg{};
	while(dlg.showDialog())
	{
		ChannelCreateInfo info{};
		if(dlg.get_info(info))
		{
			ui.channel_view->emplase_channel(&info);
			break;
		}
	}
}

void CommConversion::slot_udp_click()
{
	UDPDlg dlg{};
	while(dlg.showDialog())
	{
		ChannelCreateInfo info{};
		if(dlg.get_info(info))
		{
			ui.channel_view->emplase_channel(&info);
			break;
		}
	}
}

void CommConversion::slot_file_click()
{
	FileDirDlg dlg{};
	while(dlg.showDialog())
	{
		Logger().info("Create Channel Success");
	}
}

void CommConversion::slot_datatype_click()
{
	DataTypeDlg dlg{};
	dlg.showDialog();
	Application::Instance().cfg.save();
}

void CommConversion::slot_save_click()
{

}

void CommConversion::slot_save_as_click()
{

}

void CommConversion::slot_info_click()
{

}

void CommConversion::slot_switch_max()
{
	if(this->isMaximized())
	{
		this->showNormal();
	}
	else
	{
		this->showMaximized();
	}
}

CommConversion::~CommConversion()
{}
